package com.gorkr.labuladong.data_struct.array;

/**
 * @author gorkr
 * @date 2022/06/03 11:58
 **/
public class M5LongestPalindromicSubstring {
    public String longestPalindrome(String s){
        int[] max = new int[2], first = new int[2], second = new int[2];

        for (int i = 0; i <s.length()-1; i++) {
            first = longest(s,i,i);
            second = longest(s,i,i+1);

            max = (first[1]-first[0])>(max[1]-max[0])?first:max;
            max = (second[1]-second[0])>(max[1]-max[0])?second:max;
        }
        return s.substring(max[0],max[1]+1);

    }
    private int[] longest(String s, int l, int r){
        while (l>=0&&r<=s.length()-1){
            if(s.charAt(l)==s.charAt(r)){
                l--;r++;
            } else {
                break;
            }
        }
        return new int[]{l+1,r-1};
    }
}
    